﻿Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Shapes
Imports System.Threading
Imports System.Diagnostics

''' <summary>
''' Interaction logic for AdminWindow.xaml
''' </summary>
Partial Public Class AdminWindow
    Inherits Window
    Private messages As AwayMessageCollection
    Private settingsMon As Thread
    Private settingsEvent As New EventWaitHandle(False, EventResetMode.AutoReset, "C4F-WhileAway-Settings")
    Private userInfo As UserInfo

    Public Sub New()
        InitializeComponent()

        LoadSettings()

        settingsMon = New Thread(AddressOf MonitorMessages)
        settingsMon.IsBackground = True
        settingsMon.Start()
    End Sub

    Sub MonitorMessages()
        ' Loop waiting for reload flag
        While settingsEvent.WaitOne()
            ' Triggers the ProgressChanged event which is on the UI dispatcher thread
            Dispatcher.BeginInvoke(New Action(AddressOf LoadSettings))
        End While
    End Sub

    Private Sub LoadSettings()
        userInfo = New UserInfo()
        userInfo.Load()
        Me.DataContext = userInfo

        Dim span As TimeSpan = userInfo.ReturnTime.Subtract(DateTime.Now)
        textBoxHours.Text = Math.Max(0, span.Hours).ToString()
        textBoxMinutes.Text = Math.Max(0, span.Minutes).ToString()

        messages = New AwayMessageCollection()
        messages.LoadFromDisk()
        listBoxMessages.ItemsSource = messages
    End Sub

    Private Sub buttonLeave_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles buttonLeave.Click
        SaveSettings()

        Win32.ActivateScreensaver()
    End Sub

    Private Sub SaveSettings()
        If textBoxHours.Text = "0" AndAlso textBoxMinutes.Text = "0" Then
            userInfo.ReturnTime = DateTime.MinValue
        Else
            userInfo.ReturnTime = DateTime.Now.AddHours(Integer.Parse(textBoxHours.Text)).AddMinutes(Integer.Parse(textBoxMinutes.Text))
        End If

        userInfo.Save()
    End Sub

    Private Sub buttonClose_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles buttonClose.Click
        SaveSettings()
        Me.Close()
    End Sub

    Private Sub buttonPhotoFolderBrowse_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles buttonPhotoFolderBrowse.Click
        Dim dlg As New System.Windows.Forms.FolderBrowserDialog()
        dlg.SelectedPath = userInfo.PhotoLocation
        If dlg.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            userInfo.PhotoLocation = dlg.SelectedPath
        End If
    End Sub

    Private Sub buttonDeleteOneMessage_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles buttonDeleteOneMessage.Click
        Dim idx As Integer = listBoxMessages.SelectedIndex
        If idx > -1 Then
            messages.RemoveAt(idx)
            messages.Save()
        End If
    End Sub

    Private Sub buttonDeleteAllMessages_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles buttonDeleteAllMessages.Click
        If MessageBox.Show("Are you sure?", "Delete all messages", MessageBoxButton.YesNo) = MessageBoxResult.Yes Then
            messages.Clear()
            messages.Save()
        End If
    End Sub
End Class
